<?php
class qzone{
	public $msg;
	public $error;
	public function __construct($uin,$sid,$skey){
		$this->uin=$uin;
		$this->sid=$sid;
		$this->skey=$skey;
		$this->gtk=$this->getGTK($skey);
		$this->cookie='uin=o0'.$uin.'; skey='.$skey.';';
	}
	public function cpdel($cellid,$appid){
		$url="http://m.qzone.com/operation/operation_add?g_tk=".$this->gtk;
		$post="opr_type=delugc&res_type={$appid}&res_id={$cellid}&real_del=0&res_uin=".$this->uin."&format=json&sid=".$this->sid;
		$ua="Mozilla/5.0 (Linux; U; Android 4.0.3; zh-CN; Lenovo A390t Build/IML74K) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.8.9.457 U3/0.8.0 Mobile Safari/533.1";
		$json=$this->get_curl($url,$post,'http://m.qzone.com/infocenter?g_ut=3&g_f=6676',0,0,$ua);
		$arr=json_decode($json,true);
		if(@array_key_exists('code',$arr) && $arr['code']==0){
			$this->msg[]='删除说说'.$cellid.'成功[CP]';
		}else{
			$this->error[]='删除说说'.$cellid.'失败[CP]！原因:'.$arr['message'];
		}
	}
	public function shuodel(){
		if($shuos=$this->getnew('my')){
			foreach($shuos as $shuo){
				$appid=$shuo['comm']['appid'];
				$cellid=$shuo['id']['cellid'];
				$this->cpdel($cellid,$appid);
			}
		}
	
	}
	public function pcreply($content,$uin,$cellid,$from){
		$post="topicId={$uin}_{$cellid}__{$from}&feedsType=100&inCharset=utf-8&outCharset=utf-8&plat=qzone&source=ic&hostUin={$uin}&isSignIn=&platformid=52&uin=".$this->uin."&format=json&ref=feeds&content={$content}&richval=&richtype=&private=0&paramstr=1&qzreferrer=http://user.qzone.qq.com/".$this->uin;
		$url="http://user.qzone.qq.com/q/taotao/cgi-bin/emotion_cgi_re_feeds?g_tk=".$this->gtk;
		$json=$this->get_curl($url,$post,0,$this->cookie);
		if($json){
			$arr=json_decode($json,true);
			$arr[data][feeds]='';
			if($arr[code]==0){
				$this->msg[]='评论'.$uin.'的说说成功[PC]';
			}elseif($arr[code]==-3000){
				$this->skeyzt=1;
				$this->msg[]='评论'.$uin.'的说说失败[PC]！原因:'.$arr[message];
			}elseif($arr[code]==-10052){
				$this->msg[]='评论'.$uin.'的说说失败[PC]！原因:'.$arr[message];
			}elseif($arr[code]==-10025){
				$this->msg[]='评论'.$uin.'的说说失败[PC]！原因:'.$arr[message];
			}else{
				$this->msg[]='评论'.$uin.'的说说失败[PC]！原因'.$json;
			}
		}else{
			$this->error[]='获取评论结果失败[PC]';
		}
	}
	public function reply($content='赞'){
		if($shuos=$this->getnew()){
			foreach($shuos as $shuo){
				if($this->is_comment($this->uin,$shuo['comment']['comments'])){
					$appid=$shuo['comm']['appid'];
					$typeid=$shuo['comm']['feedstype'];
					$curkey=$shuo['comm']['curlikekey'];
					$uinkey=$shuo['comm']['orglikekey'];
					$uin=$shuo['userinfo']['user']['uin'];
					$from=$shuo['userinfo']['user']['from'];
					$cellid=$shuo['id']['cellid'];
					$this->touin=$uin;
					$this->pcreply($content,$uin,$cellid,$from);
				}
			}
		}
	}
	public function cplike($uin,$type,$uinkey,$curkey){
		$post="opr_type=like&action=0&res_uin={$uin}&res_type={$type}&uin_key={$uinkey}&cur_key={$curkey}&format=json&sid=".$this->sid;
		$url="http://m.qzone.com/praise/like?g_tk=".$this->gtk;
		$ua="Mozilla/5.0 (Linux; U; Android 4.0.3; zh-CN; Lenovo A390t Build/IML74K) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 UCBrowser/9.8.9.457 U3/0.8.0 Mobile Safari/533.1";
		$json=$this->get_curl($url,$post,'http://m.qzone.com/infocenter?g_ut=3&g_f=6676',0,0,$ua);
		$arr=json_decode($json,true);
		if(@array_key_exists('code',$arr) && $arr['code']==0){
			$this->msg[]='赞'.$uin.'的说说成功[CP]';
		}elseif($arr['code']==-11210){
			$this->error[]='赞'.$uin.'的说说失败[CP]！原因:'.$arr['message'];
		}else{
			$this->error[]='赞'.$uin.'的说说失败[CP]！原因:'.$arr['message'];
		}
	}
	public function pclike($curkey,$uinkey,$from,$appid,$typeid,$abstime,$fid){
		$cookie='uin=o0'.$this->uin.'; skey='.$this->skey.';';
		$post="qzreferrer=http://user.qzone.qq.com/".$this->uin."&opuin=".$this->uin."&unikey={$uinkey}&curkey={$curkey}&from={$from}&appid={$appid}&typeid={$typeid}&abstime={$abstime}&fid={$fid}&active=0&fupdate=1";
		$url="http://w.qzone.qq.com/cgi-bin/likes/internal_dolike_app?g_tk=".$this->gtk;
		$get=$this->get_curl($url,$post,0,$this->cookie);
		preg_match('/callback\((.*?)\)\;/is',$get,$json);
		if($json=$json[1]){
			$arr=json_decode($json,true);
			if($arr[message]=='succ' || $arr[msg]=='succ'){
				$this->msg[]='赞'.$this->touin.'的说说成功[PC]';
			}elseif($arr[code]==-3000){
				$this->skeyzt=1;
				$this->msg[]='赞'.$this->touin.'的说说失败[PC]！原因:'.$arr[message];
			}else{
				$this->msg[]='赞'.$this->touin.'的说说失败[PC]！原因'.$json;
			}
		}else{
			$this->error[]='获取赞结果失败[PC]';
		}
	}
	public function like(){
		if($shuos=$this->getnew()){
			foreach($shuos as $shuo){
				$like=$shuo['like']['isliked'];
				echo $like;
				if($like==0){
					$appid=$shuo['comm']['appid'];
					$typeid=$shuo['comm']['feedstype'];
					$curkey=$shuo['comm']['curlikekey'];
					$uinkey=$shuo['comm']['orglikekey'];
					$uin=$shuo['userinfo']['user']['uin'];
					$from=$shuo['userinfo']['user']['from'];
					$abstime=$shuo['comm']['time'];
					$cellid=$shuo['id']['cellid'];
					$this->touin=$uin;
					$this->pclike($curkey,$uinkey,$from,$appid,$typeid,$abstime,$cellid);
				}

			}
		}
	}
	public function getnew($do=''){
		if($do=='my'){
			$url="http://m.qzone.com/list?g_tk=".$this->gtk."&res_attach=att%3D10&format=json&list_type=shuoshuo&action=0&res_uin=".$this->uin."&count=20&sid=".$this->sid;
		}else{
			$url="http://m.qzone.com/get_feeds?g_tk=".$this->gtk."&res_type=0&refresh_type=2&format=json&sid=".$this->sid;
		}
		$json=$this->get_curl($url);
		$arr=json_decode($json,true);
		if(@array_key_exists('code',$arr) && $arr['code']==0){
			$this->msg[]='获取说说列表成功！';
			return $arr['data']['vFeeds'];
		}elseif($arr[message]=='请先登录'){
			$this->sidzt=1;
			$this->error[]='SID过期';
			return;
		}else{
			$this->error[]='获取说说列表失败，'.$json;
			return;
		}	
	}


	public function get_curl($url,$post=0,$referer=0,$cookie=0,$header=0,$ua=0,$nobaody=0){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		if($post){
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		}
		if($header){
			curl_setopt($ch, CURLOPT_HEADER, TRUE);
		}
		if($cookie){
			curl_setopt($ch, CURLOPT_COOKIE, $cookie);
		}
		if($referer){
			curl_setopt($ch, CURLOPT_REFERER, "http://m.qzone.com/infocenter?g_f=");
		}
		if($ua){
			curl_setopt($ch, CURLOPT_USERAGENT,$ua);
		}else{
			curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Linux; U; Android 4.0.4; es-mx; HTC_One_X Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0');
		}
		if($nobaody){
			curl_setopt($ch, CURLOPT_NOBODY,1);//主要头部
			//curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);//跟随重定向
		}
		curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
		$ret = curl_exec($ch);
		curl_close($ch);
		return $ret;
	
	}
	private function getGTK($skey){
        $len = strlen($skey);
        $hash = 5381;
        for($i = 0; $i < $len; $i++){
            $hash += ($hash << 5) + ord($skey[$i]);
        }
        return $hash & 0x7fffffff;//计算g_tk
    }
	private function is_comment($uin,$arrs){
        if($arrs){
	    	foreach($arrs as $arr){
    	   		if($arr['user']['uin'] == $uin){
        			return false;
            		break;
        		}
    		}
        	return true; 
        }else{
        	return true; 
        }
	}
	private function array_str($array){
    	$str='';
        if($array[-100]){
	        $array100=explode(' ',trim($array[-100]));
    	    $new100=implode('+',$array100);
            $array[-100]=$new100;
        }
 		foreach($array as $k=>$v){
            if($k!='-100'){
	    		$str=$str.$k.'='.$v.'&';
            }
 		}
        //$str=rtrim($str,'&');
        $str=urlencode($str.'-100=').$array[-100].'+';
        $str=str_replace(':','%3A',$str);
    	return $str;
    }
}